<template>
  <div>
    <el-form
      :model="formData"
      :rules="rules"
      ref="formDataRef"
      label-width="80px"
      @submit.prevent
    >
      <!--用户头像-->
      <el-form-item label="用户头像" prop="icon">
        <el-upload
          :headers="headers"
          class="avatar-uploader"
          :action="uploadIconUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img v-if="formData.icon" :src="ip + formData.icon" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="用户昵称" prop="username">
        <el-input
          maxlength="150"
          clearable
          placeholder="请输入昵称"
          v-model.trim="formData.username"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户性别">
        <el-switch
          v-model="formData.sex"
          class="mb-2"
          active-text="男"
          inactive-text="女"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="加入权限" prop="joinType">
        <el-switch
          v-model="formData.joinType"
          class="mb-2"
          active-text="验证加入"
          inactive-text="直接加入"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="所在地区" prop="areaName">
        <el-input
          maxlength="50"
          clearable
          placeholder="请输入地区"
          v-model.trim="formData.areaName"
        ></el-input>
      </el-form-item>
      <el-form-item label="个性签名" prop="note">
        <el-input
          clearable
          placeholder="请输入签名"
          v-model.trim="formData.note"
          type="textarea"
          :rows="5"
          maxlength="30"
          :show-word-limit="true"
          resize="none"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button type="primary" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance, computed } from "vue";
import { useUserInfoStore } from "@/stores/UserInfoStore";

const userInfoStore = useUserInfoStore();
const { proxy } = getCurrentInstance();

const props = defineProps({
  data: {
    type: Object,
  },
});
const ip = import.meta.env.VITE_APP_ACCESS_IP;
const uploadIconUrl = import.meta.env.VITE_APP_UPLOAD_IP;
const headers = ref({
  Authorization: localStorage.getItem("token"),
});
const icon = ref("");
const handleAvatarSuccess = (res, file) => {
  formData.value.icon = res.data;
};

const formData = computed(() => {
  const userInfo = Object.assign(props.data);
  return userInfo;
});
const formDataRef = ref();
const rules = {
  icon: [{ required: true, message: "请选择头像" }],
  username: [{ required: true, message: "请输入昵称" }],
};

const emit = defineEmits(["editBack"]);
const save = () => {
  formDataRef.value.validate(async (valid) => {
    if (!valid) {
      return;
    }
    const res = await proxy.Api.updateCurrentUser(formData.value);
    if (res.code !== 200) {
      proxy.$message.error(res.message);
      return;
    } else {
      proxy.$message.success("修改成功");
      userInfoStore.setInfo(res.data);
      emit("editBack");
    }
  });
};

const cancel = () => {
  emit("editBack");
};
</script>

<style lang="scss" scoped>
.avatar-uploader {
  border: 1px dashed #8c939d;
  border-radius: 6px;
  cursor: pointer;
  width: 120px;
  height: 100px;
  position: relative;
  overflow: hidden;

  .avatar {
    width: 120px;
    height: 100px;
    display: block;
  }

  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 100px;
    text-align: center;
  }
}

.info {
  margin-left: 5px;
  margin-right: 5px;
  color: #949494;
  font-size: 12px;
}
</style>
